package com.project.ssmfo.common;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.utils.AESUtil;

@ControllerAdvice
public class EcryptResponseBodyAdvice implements ResponseBodyAdvice<Object> {
 
	private static Logger logger = Logger.getLogger(EcryptResponseBodyAdvice.class);
 
    private final ObjectMapper objectMapper = new ObjectMapper();
 
    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }
 
    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter,
                                  MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass,
                                  ServerHttpRequest serverHttpRequest,
                                  ServerHttpResponse serverHttpResponse) {
        if (serverHttpRequest.getHeaders() != null && serverHttpRequest.getHeaders().size() > 0) {
            try {
            	
            	ServletServerHttpRequest ssReq = (ServletServerHttpRequest)serverHttpRequest;
            	HttpServletRequest req = ssReq.getServletRequest();
                if (req.getParameter("decrypt")!=null) {
                    if (body!=null) {
                    	String RESPONSE_KEY=Configuration.get("response_key");

                    	String jsonStr = JSONObject.toJSONString(body);

                    	String respTxt = AESUtil.encryptHex(jsonStr, RESPONSE_KEY);
                    	
                    	return respTxt;
                    }	
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
                return body;
            }
        }
        return body;
    }
 
    
    public static void main(String[] args) {
		String key = "1234567812345678";
		System.out.println(AESUtil.decryptHex("E4D2D67CA14401792AE832D58B87517F63BB58E279F1BFA5B7A7107E497CC98320776935C2D08FE278AC858AA52254D18183459CDEF44F2F5D9D84CFD898983D4E304118F25080B2A79CA63DBA62A35B2A3FE5A6D18D6D6ECABC8AA0AB0EF6F514D400EE197D60EFFD0D466DB4F652EEC5B7515ACD2FC478116E0BBD5D83C67365389A56E27ECEB1DD47B01C2557A2D786940A12DA3A3FC878E0A13B40DEBB18C87D6849BEB74DA7BA622EBF230C22EE35A9AB94A2D17F01F54199CE8C12067BE264C993F7636DCC8ADF08E83CB8DBE002D794E6AF64A92365139F35972B7B71CD5ECE381F97B013873F7BAECE412B677438ABA54B069BF05930A36415B67E02F9877FFF20D9269AE5A66D5AD941ACC965E2BBD463B67E8595DCE89557BF0D76C374B94092696ECA5069235CB3FFD8033AB10CC5A1C2DAD02032D44F933619C64D0BE3BF0247C48F38B752CE0D09BD4953226DFCF5B30F8B2C5EC7A83CEBA748FB12940EB1E8509904EF3F09D63C38F14D93EFD171566E06610F51F8F7395C646702BFE20190E4850D9EF3EBA6D661816CF8787FB434BC0EC5376447C32EC319FD2AB6C52B5801DCEF47246FE4AED7DFB7021C8AD2CBCE440C3B88003270C7DE706DB6FC31A3C7036BDF27ECF141B9C9F3775CC5BE1D1C0524B4CC0CEEAAF5898B3A1AFADAD79FEAA50904336F83B3AD81C143C2F55B3FB87F507E8B1E22601F7B8F78B4789B141504493E6D69884C3D6221682D57FDF45A2CAAB9FF653F8B9B0A6581EBDD6F333EFEFE3AE0A1690B81CFB9DD8D2A8219D511FA07D50BD0D4F58CA94374CAA2CBA5D557B6089F5EB70F687081A55D4FA455A47ACD2813E47050BE26BD48DB8C4E1AE0CD01781FA33FA552AE0F3DAF26B8CD7282505C94AF5971EBBFB88DBDFA21B83AA8F1A749F5A17F68197344DD72C2D13A7C23D388E12070D4D5E76F21671B38DDE5EC0AE76C36F147B68EDA4AB7633C3B7B16EE32B51E375F9DEB37D15D83145DD949A9D5196DFB6141C5C79EDA28B5EA2B6BB549B74BAD193383A988558FD0ABBD2E383AEB21E4D350267B206119234FCE637A2DCE2D19AC715EC25BCC30CCB24614D9A828014049464CC39A8B3CB5937FEA833D1DFB5A7A9C6B8B6301A82CAB5D807FE5857B75E1A1AB4BD45BFFE8B8D1BC7FCC93ED63C8D76A9DB34A1B7AC68EC9AE991ADA94027817E592CBC402A5A22F4217B5A579391EEED06F1A57932AE3C2F70EDA0624F73CDF3CED4E7EAEB7B2DFE474A84C148F9B9B10DF941021895E99DF963E6D8BDFC2006D76CC4B18D4D6B727B58FE34C3BC1D86034E2F27976DA60403459523C963E1F60818EAF84ABDE3225A7025B0B074490AD14BC8BB869AFDAB896DD56626DABF5C9B22DF7C951D84990DAB9A59AA67C38A990D0CC21D33E01F9481483D208F65F1DD3566BB5F8A6F142AEE11ACDC58AC5E01857E23249BB6C4D7CF10FB30D85EE33AA866066AF0438A255E0D3DA3F259A9BC9AC2038320EB4DD1AC95815B603E57E203D434EFB86E3AD7ED1BD35AF7DD2FAE3760507C421963D3BDD3A06CE18D7B2177AA3DEE40CA876EBDE1C2CA9FFE006CA77FE1EA3B0A23719C9716E345902DE23BEA7C58495113EEAE76AEB61E6B5282984209DF2B3AA5D09594F3852C1F60A17F150C48417092BF04578B5575D63C0AC4ABF8EF607DD39593974811F9C02E4C2DB76B5EEA123BA23471907ED87B0C5EFCE8FB082B64C93A04C4361D559D2EA6D02BADD600742BE9A80E9EEA838A2C6CB682B42F0AAAFE6EFFB2D94DDD439572841E1D0D24A0AEE6387DF0DB8712DAD8CA75B596C65CA895D56B3B52F26C6435AA414E859711A26BB4DBE14EF4468675888D863A0F9B0D530E9F698A61C2D9A17FCB86D5278835612F049EC5407F81BD36BEACE1AFA873D71CCFE35A33AB04661494950EA649CEA588837861BE10E2B110B6D50BD3CBCEE27C46526990C16C07F75A0FDDC2EC8182A1F0713F5C88B3C2EA1118F17033A746A6C9092FC1E687674EDD3E89F59F5FE3018DABC6D1FA285F3A8FD5943788CFA088598CBA504C31E7B31B8BC2D8D875E896AE895B44D86626C9FCA7F2FB5275498AD1D691DF7DBD60A574BDFF166ECD2A060A5BF0C5AA811AF5F7E7750CE6B894802628E5656897A93D03B50AE94B6386B24C7C13E9F65E2EA404A3CB1E66F47F34F1B8773DDA43038335E3DAA91FE49CB5EF0862D87EFA98903FA3FA9C5A1329F90E673D1BF63FF2F08FAA8DA7C01E83AB9698EBD26050B6456C4E46D7BD8F420F49A797E6C3483187087539E4DD7E561841C05BFD44D91C00DBD8ABAE724AE0A5E899ADC6602F9C98925A634957331AE2854A5198E0D88872E44DCFB40D62FC7CC6B9AE4F711046A641BDDB5306735BD20633E0FEEACD317565236BFB5BDC4CC8BDDE7F392F077E163D1A0A846AB0D44C3A6D99B35D189E86730A7EE3EA7293DFEBA99C8F0DE379020EC7F586FE3C4576A18E2BE53AB636A0584521E91B442EE069C196A5C9CED36BB941591000B9E40920DA0F1961D3CF7D8E000FC34DFA6C41296F384B5D42ECDFB441C25DB7073DC052B94A1CF0BFBCC31E45A0FA2C1FDE915FCE24BC250C5E1FE55CBA6424A552EF3F90AFB57B5A15E36C8F2037982CA81A575CA3E9F65E2EA404A3CB1E66F47F34F1B87584FC7C3442512843792FC0A5FB04B71B3B7CC7438BC0E274A5BFE5E82F9AF76F9300D8F5BBDF3C5FEAE7F68C57A727A1026191490C29854BC21090F473B147F8329D73B5507F02D363F505F8C8E264B4F6B3C375E5EE06E6BA779A2CFEA13EB7AF276F2FE859F864F3B9E5CEDF655A7720E1BB891DA9F76017FE277A9E6366B155220C27975C299D668D3564582A8E0517290A2BD5599EEE56B83B169C9C98C0AA1CB194DD3079EFB488EB615A0FCDEC131EEA5842FAEA821E217FB44398D5D7F72A2297033261C50B2BFC346E6A2E130E0A1074A0D177F755FA15FCAD9B8135869FB4E1923784BB742672EE64D8B9223A8A6CDBF725CC5180590D544477F1E90E0B4773F77FF66E883E7CB248E4BCF51C61331F211E76B30AD9A5F6579763EBD95BF253B057D0A101B0032D2AC90BBB98473AD63A9DD462DBCE13FAFD49680A1F9702BBBF4FFF462B738C45F0367D5F8E9A5FAA22678798BF9B34AE385183D67798E055D629FB855C06ACAD2997FFE109EAF665FCC9966D02C67B63F46845D02D794E6AF64A92365139F35972B7B71CD5ECE381F97B013873F7BAECE412B677438ABA54B069BF05930A36415B67E02F9877FFF20D9269AE5A66D5AD941ACC9C1E5E642900DCE11E48758EFC55BA06C695F4579312F27A359B93C6CBC3AA4923AB10CC5A1C2DAD02032D44F933619C66CF112CAA5E15C5F86103BE507E2D34D13E118215365597F8623C1CF9292DE9A224BDA00CFA3C054B55034A5BCE60F41B834C1C4A85879C8FFB53A8F7A81D4B4EFA09069015FE1D0FD56264B428DA3AA383E9DE522ECA2E3327DA95E32C1C170F70AC5246FE2023FDBEE4517409BF33C9497190EAA2653369ACBDDE4D47F1EDE3FCC66235FF5057C0F1071BDA3B991C013579C879E2237A476F8D7BA5EAC0C1BACF11A237633F6300C95925BA47A5B5A4D696A82E6F16ECE3F5974A18C49C6919805C2844947DEC45274EC2BD8936637D8D6160B2BCEF5BBC0B1318F0760B4DB1026191490C29854BC21090F473B147F8329D73B5507F02D363F505F8C8E264B4F6B3C375E5EE06E6BA779A2CFEA13EB7AF276F2FE859F864F3B9E5CEDF655A7FC2E39129EDFB1A98FF668FA7DB78826155220C27975C299D668D3564582A8E0517290A2BD5599EEE56B83B169C9C98C0AA1CB194DD3079EFB488EB615A0FCDE09985929414B8E8EBEEE226807F397E481DB273810F3E44F3B3F316630707D4DD20B45C3C806E92D449B22291F4156C09E1087E13243358273442B02EFAB9BB4F3C03BA723E4A29F387093239946321774DCFA7F0B0D81614FB5CB09A1E84FB8B9317244EBE509888D0CFE13CDA00E64B5C45A46F128C9C7DECE05C29A45CD0026ED50525B3F254514BAEF6590ADC1C5DF14E4B378C723D3CAA0F86EE55D153E124F47EA7A303ADBE0FFDAAC1CB31C1D8B0E82AB40DB06575F6C77E98AA1697234AB6FC1EF6F5F540CF0C9592DFB444E6588835397A963A6B465FD8A3647B1A0A5486FF111DDFE93AC69B5DDFAC92319CBC8DE0CF876CD6692DAD2456013919C22166C074D6781F9C0758F7A7984B44E30934777E53212E8E0011FE468745EB91CB234E277444D03F53CB6E225F304D7379EC460919960AB204BB4FAC61B73469BE7D8F3C75B51C5114EEC7D6E538886B6090F24B878EB081F0D57D261C7DA27A28F24DE27B4240E2D8335614D45432C7132F6DE7877435CAF63DA2842928BA6321223136367BD6E5CFB852FE854F7A8E290A8F5B0DFAE561F66E2C8048D48B7B657A4F37A8101E7D5C64302061EB617D6B5BA30DA3F71D24C0A4815F22000B04C20424D0B84370B0F7C9323258F254C358664DAB3D29250D16819EBA08B776F9CE00D8C23B9265FF37D82974EA01DF606841400A5B4B5A8A48181E619CAFB8337597618793192F012A424010DE7366C54451899F932694AB3B654430A1B6D508CA94374CAA2CBA5D557B6089F5EB70F687081A55D4FA455A47ACD2813E47050BE26BD48DB8C4E1AE0CD01781FA33FA552AE0F3DAF26B8CD7282505C94AF597171D5BF53A21F57567193EFE6A450244368197344DD72C2D13A7C23D388E12070D4D5E76F21671B38DDE5EC0AE76C36F189AFF83E90CBB22F32D6AF48DF9EC7169F089853100BED39DBA9D49FB8717390BBDE324FFFE57AEACF530BEC49570C4CE73DF52C9465588CB13EA6C676ECC6979527778EF5753CDA7E4DE544B3C8B266FC1E687674EDD3E89F59F5FE3018DABC7532D39FAD056F06ADFE873E07A21DAA6AAEF3647AA41C343B04BD9A5E57DD3E6BE17D164C06C18354DE49CAB366EA2AFA2386D8DF2DCC8C6E68D2109D0526A2B522763D9047C7BAFB85389411E8C1EFF06489D543E98CEE55082DE1BF66B905559294D640B29E96FD6443C81F9E91DBE8AFE786F6A5D1A2F234BE5292A104EE3FBD23479FAC2E951D32BF667E8CD90E3A26E2DF9B919DF3074BB4E33659DBA0A835D4E77548D4BB5DF78D9E4D6F1A69EE7B127A4A54E1E62AAC2E7F8D9480B4", key));
	}
    
}